<?php

require_once ("Money.class.php");
require_once ("MoneyFactory.class.php");
require_once ("MoneyExceptions.php");

class PorteMonnaie {

 	private $_otherCurrencies = array();

    public static function add($m1,$m2) {
		
		$exc=new IncompatiblesCurrenciesException("incompatible currencies");
		
		if (!$m1->sameCurrency($m2)) {
			throw new IncompatiblesCurrenciesException();
		}
	        
	        $_factory=MoneyFactory::getDefaultFactory();
	    	
	    	return $_factory->createMoney($m1->getValue()+$m2->getValue(),$m1->getCurrency());	
    }


    public static function suppr($m1,$m2) {
		
		$exc=new IncompatiblesCurrenciesException("incompatible currencies");
		$excMoney=new NotEnoughMoneyException("Not Enough Money");
		
		if (!$m1->sameCurrency($m2)) {
			throw new IncompatiblesCurrenciesException();
		}
		
		if ($m1->getValue()<$m2->getValue()) {
			throw new NotEnoughMoneyException();
		}

	        $_factory=MoneyFactory::getDefaultFactory();
	    	
	    	return $_factory->createMoney($m1->getValue()-$m2->getValue(),$m1->getCurrency());	
    }

     public static function addMulti($money,$wallet) {

     
     $res=$wallet;
	  foreach (MoneyFactory::getDefaultFactory()->getCurrencies() as $key => $currenciesPossible) {

  			if(strcmp($currenciesPossible,$money->getCurrency())==0){
  				$i=0;
				foreach ($wallet as $key => $cur) {
					if(strcmp($cur->getCurrency(),$money->getCurrency())==0){
						$res[$i] = PorteMonnaie::add($cur,$money);
					}
					$i++;
				}
  			}
  			else{
				//On met de coté la devise

  			}
  		}

  		return $res;
    }


  public static function supprMulti($money,$wallet) {

  	$res = $wallet;
		
	  foreach (MoneyFactory::getDefaultFactory()->getCurrencies() as $key => $currenciesPossible) {
  			if(strcmp($currenciesPossible,$money->getCurrency())==0){
  				$i=0;
				foreach ($wallet as $key => $cur) {
					if(strcmp($cur->getCurrency(),$money->getCurrency())==0){
						$res[$i]=PorteMonnaie::suppr($cur,$money);
					}
					$i++;
				}
  			}
  			else{
				
				//On met de coté la devise

  			}
  		}

  		return $res;
    }



}
?>
